import * as cc from "cc";
import { VMCustomEntity } from "./VMCustomEntity";

export class VMChildEntity extends VMCustomEntity{
    // @ts-ignore
    observerAttr: VMChildAttr; 
    
    seekNodeByName(root: cc.Node, name: string): cc.Node {
        if (!root)
            return null;

        if (root.name == name)
            return root;
        let arrayRootChildren = root.children;
        let length = arrayRootChildren.length;
        for (let i = 0; i < length; i++) {
            let child = arrayRootChildren[i];
            let res = this.seekNodeByName(child, name);
            if (res != null)
                return res;
        }
        return null;
    }

    _updateValue(newValue,oldValue,watchPath){
        let _onChangeEvent = this.observerAttr.formator;
        if(!_onChangeEvent){
            console.warn(`VMFactory->VMEventEntity [${this.node.name}] have not onChangeEvent, emit event fail !`);            
            return;
        }
        let child = this.seekNodeByName(this.node,this.observerAttr.child);

         _onChangeEvent.call(this.controllerUserComponent,{
            child,
            entity: this,
            newValue,
            oldValue,
            node: this.node,
            nodeIdx :0,
            watchPath,
        });
        // _onChangeEvent({
        //     newValue,
        //     oldValue,
        //     node: this.node,
        //     nodeIdx :0,
        //     watchPath
        // });
    }
    onCheckProperty(): boolean {
        return true;
    }
}
